package com.ninetaildemonfox.zdl.socialproject.utils.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.VipContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.VipPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.adapter.PayAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.bean.PayNamePayBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/19 11:44
 * 功能描述：支付dialog
 * 联系方式：1037438704@qq.com
 */
public class PayDialog extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, VipContract {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.text_determine)
    TextView textDetermine;
    @BindView(R.id.text_private_chat)
    TextView textPrivateChat;
    @BindView(R.id.image_bottom_finish)
    ImageView imageBottomFinish;
    private List<PayNamePayBean> list;
    private PayAdapter payAdapter;
    private VipPresenter vipPresenter;
    private String money, type, member_id;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_pay_dialog;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        vipPresenter = new VipPresenter(this, me);
        money = getIntent().getExtras().getString("money");
        member_id = getIntent().getExtras().getString("member_id");
        type = getIntent().getExtras().getString("type");
        textPrivateChat.setText("￥" + money);
        if (money == null) {
            ToastUtil.show("没有获取到金额！");
            finish();
            return;
        }
        initView();
        initData();
    }

    private void initData() {
        list.add(new PayNamePayBean("钱包支付", false, R.mipmap.ci_pay_wallet));
        list.add(new PayNamePayBean("支付宝支付", false, R.mipmap.ic_pay_alipay));
        list.add(new PayNamePayBean("微信支付", false, R.mipmap.ic_pay_wx));
        payAdapter.setNewData(list);
        payAdapter.setOnItemClickListener(this);
    }

    private void initView() {
        list = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        payAdapter = new PayAdapter(R.layout.list_dialog_pay);
        recyclerView.setAdapter(payAdapter);
    }

    @OnClick({R.id.text_determine, R.id.image_bottom_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_determine:
                pay();
                break;
            case R.id.image_bottom_finish:
                finish();
                break;
            default:
        }
    }

    int payCount = -1;

    private void pay() {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getChoice() == true) {
                payCount = i;
            }
        }
        if (payCount == -1){
            ToastUtil.show("请选择支付方式");
            return;
        }

//        1聊天或查看社交账号 2发布动态 4解锁相册 5开通会员 7发布约会
        //立即开通
        MessageDialog.show(me, "使用" + list.get(payCount).name, "", "确认", "取消")
                .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                    @Override
                    public boolean onClick(BaseDialog baseDialog, View v) {
                        if (payCount == 0) {
//                                    钱包支付
                            WaitDialog.show(me, "数据请求中");
                            vipPresenter.balancePay(token, member_id, type, money, "");
                        } else if (payCount == 1) {
//                                    支付宝支付
                            vipPresenter.aliPay(token, member_id, type, money, "");
                        } else {
//                                    微信支付
                            vipPresenter.wxPay(token, member_id, type, money, "");
                        }
                        return false;
                    }
                });
    }

    public static void start(Context me, String money, String type, String member_id) {
        Intent intent = new Intent(me, PayDialog.class);
        Bundle bundle = new Bundle();
        bundle.putString("money", money);
        bundle.putString("member_id", member_id);
        bundle.putString("type", type);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    public static void start(Context me, String money, String type, String member_id,String map) {
        Intent intent = new Intent(me, PayDialog.class);
        Bundle bundle = new Bundle();
        bundle.putString("money", money);
        bundle.putString("member_id", member_id);
        bundle.putString("type", type);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }



    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<PayNamePayBean> data = payAdapter.getData();
        PayNamePayBean payNamePayBean = data.get(position);
        if (payNamePayBean.getChoice() == true) {
            payNamePayBean.setChoice(false);
        } else {
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setChoice(false);
            }
            payNamePayBean.setChoice(true);
        }
        payAdapter.notifyDataSetChanged();
    }

    @Override
    public void Success(Map<String, String> data) {
        MessageDialog.show(me
                , "提示"
                , data.get("message")
                , "知道了").setOkButton(new OnDialogButtonClickListener() {
            @Override
            public boolean onClick(BaseDialog baseDialog, View v) {
                me.finish();
                return false;
            }
        });
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }
}
